// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Control from "./Control";
import KnifeBag from "./KnifeBag";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Stage4 extends cc.Component {

    @property(cc.Node)
    targetNode: cc.Node = null;

    @property(cc.Node)
    knifeNode: cc.Node = null;

    @property(cc.Label)
    timeOutLabel: cc.Label = null;

    @property(cc.Prefab)
    knifePrefab: cc.Prefab = null;

    @property({displayName:"标靶转速"})
    targetRotation: number = 0;

    @property({displayName:"倒计时"})
    timeOut: number = 0;

    canThrow: boolean = true;

    knifeNodeArr: cc.Node[] = null;

    // LIFE-CYCLE CALLBACKS:

    timeOutIntervalId :number = 0;

    onLoad () {

        this.canThrow = true;
        this.targetNode.zIndex = 1;
        this.knifeNodeArr = [];

        this.timeOutLabel = cc.find('Canvas/关卡/倒计时').getComponent(cc.Label);

        setInterval(()=> {

            //Control.inst.changeDirAndSpeed();

            let dir = Math.random() > 0.5 ? 1 : -1;
            let speed = 1 + Math.random() * 6;
    
            this.targetRotation = dir * speed;

        }, 2000)

        this.timeOutIntervalId = setInterval(()=> {

            if(this.timeOut > 0) {

                this.timeOut -= 1;

                cc.log(this.timeOut);

                this.timeOutLabel.string = this.timeOut + '';
            }
            else {
           
                cc.log("时间到，游戏结束");
  
                // 退出倒计时定时
                clearInterval(this.timeOutIntervalId);

                cc.director.loadScene('MainScene');
            }

        }, 1000)

        this.node.on('touchstart', this.onTouchStart, this);
    }

    onTouchStart() {

        this.throwKnife();
    }

    throwKnife() {

        if (this.canThrow) {

            this.canThrow = false;

            // 飞刀袋里的飞刀减1
            KnifeBag.inst.consume(1);
            cc.log("当前飞刀袋里飞刀的数量", KnifeBag.inst.count);

            this.knifeNode.runAction(cc.sequence(

                //飞刀飞向标靶
                cc.moveTo(0.15, cc.v2(this.knifeNode.x, this.targetNode.y - this.targetNode.width / 2)),
                cc.callFunc(()=> {
                    
                    let isHit = false;
                    let gap = 15;

                    for (let knifeNode of this.knifeNodeArr) {

                        if (Math.abs(knifeNode.angle) < gap || (360 - knifeNode.angle) < gap) {

                            isHit = true;
                            break;
                        }
                    }

                    if (isHit) {

                        // 碰到其他中靶飞刀后

                        this.knifeNode.runAction(cc.sequence(

                            cc.spawn(
                                cc.moveTo(0.25, cc.v2(this.knifeNode.x, -cc.winSize.height)),
                                cc.rotateTo(0.25, 30)
                            ),

                            cc.callFunc(()=> {

                                cc.log("游戏结束");

                                // 退出倒计时定时
                                clearInterval(this.timeOutIntervalId);

                                cc.director.loadScene('MainScene');
                            })
                        ));
                    }
                    else {

                        let knifeNode = cc.instantiate(this.knifePrefab);
                        knifeNode.setPosition(this.knifeNode.position);
                        this.node.addChild(knifeNode);
    
                        this.knifeNode.setPosition(cc.v2(0, -300));
    
                        this.knifeNodeArr.push(knifeNode);
    
                        this.canThrow = true;
                    }
                })
            ));

            // 当飞刀袋里的飞刀数为0时,即全部飞刀都已中靶(如果有飞刀没有中靶,游戏会直接结束),进入下一关卡
            // 暂时先实现, 飞刀数小于0时, 即第10刀也中靶, 然后再点击一次进入下一关
            if(KnifeBag.inst.count < 0) {

                // 退出倒计时定时
                clearInterval(this.timeOutIntervalId);

                cc.director.loadScene('VictoryScene');
            }
        }
    } 
    
    start () {

    }

    onDestroy() {

        this.node.off('touchstart', this.onTouchStart, this);
    }

    update (dt) {    //Control.inst.targetRotation

        this.targetNode.angle = (this.targetNode.angle + this.targetRotation) % 360;

        for (let knifeNode of this.knifeNodeArr) {

            knifeNode.angle = (knifeNode.angle + this.targetRotation) % 360;
            
            let rad = Math.PI * (knifeNode.angle - 90)/ 180;
            let r = this.targetNode.width / 2;
            knifeNode.x = this.targetNode.x + r * Math.cos(rad);
            knifeNode.y = this.targetNode.y + r * Math.sin(rad);
        }
    }
}
